package com.vrv.utils;

import java.sql.DriverManager;
import java.sql.SQLException;

import org.apache.log4j.Logger;

public class JdbcUtil {

    private final Logger logger = Logger.getLogger(JdbcUtil.class);

    private static JdbcUtil jdbc = null;

    private JdbcUtil() {

    }

    public static synchronized JdbcUtil getInstance() {
        if (null == jdbc) {
            jdbc = new JdbcUtil();
        }
        return jdbc;
    }

    public boolean getConnect(String type, String ip, String port, String dbname, String username,
            String password) {
        boolean flag = false;
        try {
            String url = "";
            if ("mysql".equals(type)) {
                // 加载MySql的驱动类
                Class.forName("com.mysql.jdbc.Driver");
                url = "jdbc:mysql://" + ip + ":" + port + "/" + dbname;
            }
            DriverManager.getConnection(url, username, password);
            flag = true;
        } catch (ClassNotFoundException e) {
            logger.error("找不到驱动程序类 ，加载驱动失败！", e);
        } catch (SQLException e) {
            logger.error("数据连接失败！", e);
        }
        return flag;
    }

}
